/**
 * 系统区域组件
 *生成组件调用 createRegion("regionDiv",false,onRegionClick);
 * 参数：1.控件ID
 * 		 2.否显示全国的所有区域，否-从数据字典配置sysArea取当前区域
 * 		 3.控件点击事件回调函数 function(e){console.log('当前区域代码',e.code);console.log('当前是否为【全部】区域',e.all)}
 * **/

function createRegion(id,isAll,onClick){
	$("#"+id).html("");
	$("#"+id).append('<div id="'+id+'_proDiv" style="padding-bottom:5px;"></div>');
	$("#"+id).append('<div id="'+id+'_cityDiv" style="padding-bottom:5px;"></div>');
	$("#"+id).append('<div id="'+id+'_areaDiv" style="padding-bottom:5px;"></div>');
	
	var code="";
	var allStr = "";
	var className = ".region-area";
	if(!isAll){//是否显示全国的所有区域，否-从数据字典配置sysArea取当前区域
		code=loadRegionConfig();
	}
	var regions=getRegionByCode(code);
    //
	if(code==""){//全国
        allStr = "allProvince";
        className = ".region-province";
		var regionContent="";
		for(var i=0;i<regions.length;i++){
			regionContent+="&nbsp;&nbsp;<a class='region-province' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+regions[i].code+"\","+onClick+")' >"+regions[i].name+"</a>";
		}
		$("#"+id+"_proDiv").html("<label>省：</label>&nbsp;&nbsp;<a class='region-province' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+code+"\","+onClick+",\"allProvince\")' >全部</a>"+regionContent);
	}else if(code.indexOf("0000") != -1){//全省
        allStr = "allCity";
        className = ".region-city";
		var regionContent="";
		for(var i=0;i<regions.length;i++){
			regionContent+="&nbsp;&nbsp;<a class='region-city' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+regions[i].code+"\","+onClick+")' >"+regions[i].name+"</a>";
		}
		$("#"+id+"_cityDiv").html("<label>市：</label>&nbsp;&nbsp;<a class='region-city' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+code+"\","+onClick+",\"allCity\")' >全部</a>"+regionContent);
	}else if(code.indexOf("00") != -1){//全市
        allStr = "allArea";
		var regionContent="";
		for(var i=0;i<regions.length;i++){
			regionContent+="&nbsp;&nbsp;<a class='region-area' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+regions[i].code+"\","+onClick+")' >"+regions[i].name+"</a>";
		}
		$("#"+id+"_areaDiv").html("<label>区域：</label>&nbsp;&nbsp;<a class='region-area' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+code+"\","+onClick+",\"allArea\")' >全部</a>"+regionContent);
	}else{//区
		var regionContent="";
		for(var i=0;i<regions.length;i++){
			regionContent+="&nbsp;&nbsp;<a class='region-area' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+regions[i].code+"\","+onClick+")' >"+regions[i].name+"</a>";
		}
		$("#"+id+"_areaDiv").html("<label>区域：</label>"+regionContent);
	}


    $(className).each(function(index,value){
        if(index==0){
        	if (allStr == "") {
                clickArea(this,id,code,onClick);
			} else {
                clickArea(this,id,code,onClick,allStr);
			}

        }
    });
}

function clickArea(e,id,code,onClick,all){
	if(	$(e).hasClass("region-province")){
		$(".region-province").removeClass("region-clicked");
	}else if($(e).hasClass("region-city")){
		$(".region-city").removeClass("region-clicked");
	}else if($(e).hasClass("region-area")){
		$(".region-area").removeClass("region-clicked");
	}
	$(e).addClass("region-clicked");
	if(all!=undefined){//点击全部标签时
		if(all=="allProvince"){//全省
			$("#"+id+"_cityDiv").html("");
			$("#"+id+"_areaDiv").html("");
		}else if(all=="allCity"){//全市
			$("#"+id+"_areaDiv").html("");
		}
		onClick({all:true,code:code});//触发点击事件
	}else{//点击某个区域时
		var regions=getRegionByCode(code);
		var regionContent="";
		if(code.indexOf("0000")!=-1){//省
			for(var i=0;i<regions.length;i++){
				regionContent+="&nbsp;&nbsp;<a class='region-city' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+regions[i].code+"\","+onClick+")' >"+regions[i].name+"</a>";
			}
			$("#"+id+"_cityDiv").html("<label>市：</label>&nbsp;&nbsp;<a class='region-city' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+code+"\","+onClick+",\"allCity\")' >全部</a>"+regionContent);
			$("#"+id+"_areaDiv").html("");
		}else if(code.indexOf("00")!=-1){//市
			for(var i=0;i<regions.length;i++){
				regionContent+="&nbsp;&nbsp;<a class='region-area' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+regions[i].code+"\","+onClick+")' >"+regions[i].name+"</a>";
			}
			$("#"+id+"_areaDiv").html("<label>区：</label>&nbsp;&nbsp;<a class='region-area' href='javascript:void(0)' onclick='clickArea(this,\""+id+"\",\""+code+"\","+onClick+",\"allArea\")' >全部</a>"+regionContent);
		}else{
			
		}
		onClick({all:false,code:code});//触发点击事件

		var str = "";
		var allStr = "";
		if (code.indexOf("0000")!=-1) {
            str = ".region-city";
            allStr = "allCity";
		} else if(code.indexOf("00")!=-1) {
            str = ".region-area";
            allStr = "allArea";
		}

		if (str != "") {
            // 选中全部
            $(str).each(function(index,value){
                if(index==0){
                    clickArea(this,id,code,onClick,allStr);
                }
            });
		}
	}

	
}

//数据字典中设置的系统当前区域配置
function loadRegionConfig(){
	var code="";
	$.ajax({
  	  url : ctx+"/region/getRegionConfig.do",
  	  type : "post",
      async:false,
      success: function (text) {
    	  code=text;
      }
	});
	return code;
}

//根据code获取当前区域下一级区域列表
function getRegionByCode(code){
	var data=[];
	$.ajax({
  	  url : ctx+"/region/getRegionByCode.do",
  	  type : "post",
  	  data:{
  		  code:code
  	  },
      async:false,
      success: function (text) {
    	  data=text;
      }
	});
	return data;
}
